<?php

namespace MLM\Model\Product\Stock;


use MLM\Model\Product\Stock;

class Storage extends \MLM\Model\Generic\Db\Storage {
    
    public function save(Stock $stock){
        $insert = $this->_sql->insert()
                ->into('product_stock')
                ->values($stock->pull());
        
        $result = $this->execute($insert);
        
        return $result;
    }

    public function edit($dataStock, $where){

        $update = $this->_sql->update()
                ->table('product_stock')
                ->set($dataStock)
                ->where($where);

        $result = $this->execute($update);

        return $result;

    }
    

    public function del($stockId){

        $stmt = $this->_db->query("delete from product_stock where product_stock_id = ? ");
        return $stmt->execute(array($stockId)) ;

    }


    public function updateStock($prodId, $memberId, $opr, $qty){

        $stmt = $this->_db->query("update product_stock set qty = qty".$opr.$qty." where product_id = ? and member_id = ? ");
        return $stmt->execute(array($prodId, $memberId)) ;

    }

    public function getMemberStock($prodId, $memberId){
        $stmt = $this->_db->query("select * from product_stock where product_id = ? and member_id = ?");
        return $stmt->execute(array($prodId, $memberId)) ;
    }

    public function getProductStockList($prodId, $memberId, $searchField, $start, $limit){

        $sql = "select count(1) as total
                from product p, product_stock ps, supliers s, product_price pp
                where ps.product_id = p.product_id and s.suplier_id = p.suplier_id
                and pp.product_id = p.product_id ";

        if ($prodId != ""){
            $sql .= " and ps.product_id = $prodId ";
        }

        if ($memberId != ""){
            $sql .= " and member_id = $memberId ";
        }

        if ($searchField !=""){
            $sql .= " and $searchField";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute();

        while($getTotal->next()){
                $res = $getTotal->current();
                $rows = $res['total'];
        }

        $data = array($rows);



        $sql = "select * from 
                product p, product_stock ps, members m, supliers s, product_price pp
                where ps.product_id = p.product_id and m.member_id = ps.member_id
                and s.suplier_id = p.suplier_id and pp.product_id = p.product_id ";

        if ($prodId != ""){
            $sql .= " and ps.product_id = $prodId ";
        }

        if ($memberId != ""){
            $sql .= " and ps.member_id = $memberId ";
        }

        if ($searchField !=""){
            $sql .= " and $searchField";
        }

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }

        

        $stmt = $this->_db->query($sql);
        $getStock = $stmt->execute();

        $dataStock = array();
        while($getStock->next()){
                $res = $getStock->current();

                $aksi = "<a href='javascript: void(0)' onclick='del(".$res['product_stock_id'].")'>Hapus</a>";

                array_push($dataStock, array("kodeProduk" => $res["product_code"],
                                             "namaProduk" => $res["product_name"],
                                             "suplier" => $res["suplier_name"],
                                             "agen" => '('.$res['member_code'].') '.$res['member_name'],
                                             "harga" =>  $res['agent_price'],
                                             "qty" => $res['qty'],
                                             "Aksi" => $aksi));

        }

        array_push($data, $dataStock);

        return $data;
    }
}
